package com.oh.core.common.utils;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;

/**
 *
 */
public class NettyAttrUtils {

    private final static AttributeKey<String> CLIENT_ID = AttributeKey.valueOf("CLIENT_ID");

    /**
     * 将客户端ID存入通道中
     * @param channel
     * @param clientId
     */
    public static void putClientId(Channel channel, String clientId) {
        channel.attr(CLIENT_ID).set(clientId);
    }

    /**
     * 从通道中获取客户端ID
     * @param channel
     * @return
     */
    public static String getClientId(Channel channel) {
        return (String) getAttribute(channel, CLIENT_ID);
    }

    /**
     * 判断是否有客户端ID
     * @param channel
     * @return
     */
    public static boolean hasClientId(Channel channel) {
        return getAttribute(channel, CLIENT_ID) != null;
    }

    /**
     * 从通道中获取属性
     * @param channel
     * @param key
     * @param <T>
     * @return
     */
    public static <T> Object getAttribute(Channel channel, AttributeKey<T> key){
        return channel.attr(key).get();
    }
}
